//
//  CCForgetPassWordViewController.m
//  cc
//
//  Created by ZLQ on 2016/10/25.
//  Copyright © 2016年 hbyj. All rights reserved.
//

#import "CCForgetPassWordViewController.h"
#import "SVProgressHUD.h"
#import "AFNetworking.h"
@interface CCForgetPassWordViewController ()
{
    UIView *bgView;
    UINavigationBar *customNavgationBar;
    UITextField *code;
    //获取验证码
    UIButton *yzButton;
}
/**
 *  保存电话号码
 */

@property(nonatomic,copy)NSString *oUserPhoneNum;
/**
 *  时间秒数
 */
@property(nonatomic,assign)NSInteger timeCount;
/**
 *  倒计时
 */
@property(nonatomic,strong)NSTimer *timer;
/**
 *  验证码
 */
@property(nonatomic,copy)NSString *smsId;
/**
 *  输入电话号码的文本框
 */
@property(nonatomic,strong)UITextField *phone;
@end

@implementation CCForgetPassWordViewController

- (void)viewDidLoad {
    [super viewDidLoad];
    self.title=@"找回密码1/2";
    //背景颜色
    self.view.backgroundColor=[UIColor colorWithRed:240/255.0f green:240/255.0f blue:240/255.0f alpha:1];
    //返回按钮
    UIBarButtonItem *addBtn=[[UIBarButtonItem alloc]initWithTitle:@"" style:UIBarButtonItemStylePlain target:self action:@selector(clickaddBtn)];
    //按钮图片
    [addBtn setImage:[UIImage imageNamed:@"v2_goback"]];
    [addBtn setImageInsets:UIEdgeInsetsMake(0, -15, 0, 15)];
    //按钮颜色
    addBtn.tintColor=[UIColor colorWithRed:248/255.0f green:144/255.0f blue:34/255.f alpha:1];
    //添加按钮到导航栏
    [self.navigationItem setLeftBarButtonItem:addBtn];
    
    UIButton *button = [[UIButton alloc] initWithFrame:CGRectMake(10, 40, 50, 20)];
    [button setTitle:@"返回" forState:UIControlStateNormal];
    [button setTitleColor:[UIColor orangeColor] forState:UIControlStateNormal];
    [button setTitleColor:[UIColor redColor] forState:UIControlStateHighlighted];
    [button addTarget:self action:@selector(clickaddBtn) forControlEvents:UIControlEventTouchUpInside];
    [self.view addSubview:button];
    
    [self createTextFields];
}

//实现按钮监听方法--点击返回上一级菜单
-(void)clickaddBtn
{
    //[self.navigationController popViewControllerAnimated:YES];
    [self dismissViewControllerAnimated:YES completion:nil];
}

/**
 *  添加文本框--输入手机号码
 */
-(void)createTextFields
{
    //初始化标签，确定位置
    UILabel *label=[[UILabel alloc]initWithFrame:CGRectMake(30, 75, self.view.frame.size.width-90, 30)];
    label.text=@"请输入您的手机号码";
    label.textColor=[UIColor grayColor];
    label.font=[UIFont systemFontOfSize:13];
    [self.view addSubview:label];
    
    
    CGRect frame=[UIScreen mainScreen].bounds ;
    
    bgView=[[UIView alloc]initWithFrame:CGRectMake(10, 110, frame.size.width-20, 100)];
    bgView.layer.cornerRadius=3.0;
    bgView.backgroundColor=[UIColor whiteColor];
    [self.view addSubview:bgView];
    
    self.phone=[self creatTextFieldFrame:CGRectMake(100, 10, 200, 30) font:[UIFont systemFontOfSize:14] placeholder:@"11位手机号码"];
    //全部删除
    self.phone.clearButtonMode =UITextFieldViewModeWhileEditing;
    //键盘样式
    self.phone.keyboardType = UIKeyboardTypeNumberPad;
    code=[self creatTextFieldFrame:CGRectMake(100, 60, 200, 30) font:[UIFont systemFontOfSize:14] placeholder:@"6位数字"];
    //密文样式
    //code.secureTextEntry = YES;
    //键盘样式--数字键盘
    code.keyboardType = UIKeyboardTypeNumberPad;
    
    UILabel *phoneLabel = [[UILabel alloc] initWithFrame:CGRectMake(20, 12, 50, 25)];
    phoneLabel.text = @"手机号";
    phoneLabel.textColor = [UIColor blackColor];
    //左对齐
    phoneLabel.textAlignment =  NSTextAlignmentLeft;
    //字号
    phoneLabel.font = [UIFont systemFontOfSize:14];
    
    UILabel *codeLabel = [[UILabel alloc] initWithFrame:CGRectMake(20, 62, 50, 25)];
    codeLabel.text=@"验证码";
    codeLabel.textColor=[UIColor blackColor];
    codeLabel.font=[UIFont systemFontOfSize:14];
    
    yzButton=[[UIButton alloc]initWithFrame:CGRectMake(bgView.frame.size.width-100, 62, 100, 30)];
    [yzButton setTitle:@"获取验证码" forState:UIControlStateNormal];
    //设置字号大小
    yzButton.titleLabel.font = [UIFont systemFontOfSize:14];
    [yzButton setTitleColor:[UIColor colorWithRed:248/255.0f green:144/255.f blue:34/255.f alpha:1] forState:UIControlStateNormal];
    [yzButton addTarget:self action:@selector(getValidCode:) forControlEvents:UIControlEventTouchUpInside];
    
    UIImageView *line1=[self createImageViewFrame:CGRectMake(20, 50, bgView.frame.size.width-40, 1) imageName:nil color:[UIColor colorWithRed:180/255.0f green:180/255.0f blue:180/255.0f alpha:0.3]];
    //下一步
    UIButton *landBtn=[self createButtonFrame:CGRectMake(10, bgView.frame.size.height+bgView.frame.origin.y+30, self.view.frame.size.width-20, 37) backImageName:nil title:@"下一步" titleColor:[UIColor whiteColor] font:[UIFont systemFontOfSize:17] target:self action:@selector(landClick)];
    landBtn.backgroundColor=[UIColor colorWithRed:248/255.0f green:144/255.0f blue:34/255.0f alpha:1];
    landBtn.layer.cornerRadius=5.0f;
    
    [self.view addSubview:landBtn];
    
    
    [bgView addSubview:line1];
    [bgView addSubview:code];
    [bgView addSubview:phoneLabel];
    [bgView addSubview:self.phone];
    [bgView addSubview:codeLabel];
    [bgView addSubview:yzButton];
    
}

//点击‘获取验证码’
-(void)getValidCode:(UIButton *)sender
{
    if ([_phone.text isEqualToString:@""]) {
        [SVProgressHUD showInfoWithStatus:@"亲,请输入手机号码"];
        return;
    }else if (_phone.text.length<11){
        [SVProgressHUD showInfoWithStatus:@"您输入的手机号码格式不正确"];
        return;
    }
    //输入手机号，请求发送验证码
    
    NSDictionary *parmar = @{@"phone":self.phone.text};
    
    AFHTTPSessionManager *manager = [AFHTTPSessionManager manager];
    [manager POST:@"http://text.dawenkeji.com/Validate.asmx/SendYZM_ZHMM" parameters:parmar progress:^(NSProgress * _Nonnull uploadProgress) {
        
    } success:^(NSURLSessionDataTask * _Nonnull task, id  _Nullable responseObject) {
        
        NSLog(@"上传成功-->%@",responseObject);
    } failure:^(NSURLSessionDataTask * _Nullable task, NSError * _Nonnull error) {
        
        NSLog(@"上传失败-->%@",error);
        
    }];
    
    sender.userInteractionEnabled = NO;
    
    //倒计时 秒数
    self.timeCount = 60;
    __weak CCForgetPassWordViewController *weakSelf = self;
    weakSelf.timer = [NSTimer scheduledTimerWithTimeInterval:1 target:self selector:@selector(reduceTime:) userInfo:sender repeats:YES];
}

/**
 *  点击‘下一步’ 跳转
 */
-(void)landClick
{
    if ([self.phone.text isEqualToString:@""]) {
        [SVProgressHUD showInfoWithStatus:@"亲，请输入手机号码"];
        return;
    }else if (self.phone.text.length<11 || self.phone.text.length>=12){
        [SVProgressHUD showInfoWithStatus:@"请检查手机号码位数是否正确"];
        return;
    }else if ([code.text isEqualToString:@""]){
        [SVProgressHUD showInfoWithStatus:@"请输入验证码"];
        return;
    }else if (code.text.length<6 || code.text.length>=7){
        [SVProgressHUD showInfoWithStatus:@"验证码错误"];
        return;
    }
    
    NSDictionary *parmar = @{@"phone":self.phone.text,@"yzm":code.text};
    AFHTTPSessionManager *manager = [AFHTTPSessionManager manager];
    [manager POST:@"http://text.dawenkeji.com/Validate.asmx/ZhmmYZM" parameters:parmar progress:^(NSProgress * _Nonnull uploadProgress) {
        
    } success:^(NSURLSessionDataTask * _Nonnull task, id  _Nullable responseObject) {
        
        NSLog(@"上传成功%@",responseObject);
        
        if (responseObject[@"phone"]==_phone.text && responseObject[@"yzm"]==code.text) {
        /*
            CCNewPassWardViewController *new = [[CCNewPassWardViewController alloc] init];
            //赋值
            new.userPhone = _phone.text;
            [self.navigationController pushViewController:new animated:YES];
        */
        }
       
    } failure:^(NSURLSessionDataTask * _Nullable task, NSError * _Nonnull error) {
        NSLog(@"上传失败%@",error);
    }];
}
/**
 *  实现计时器监听方法
 */
-(void)reduceTime:(NSTimer *)codeTimer
{
    //时间减一
    self.timeCount--;
    //重新获取验证码
    if (self.timeCount == 0) {//当计时器归零
        [yzButton setTitle:@"重新获取验证码" forState:UIControlStateNormal];
        //设置‘获取验证码’的字体颜色
        [yzButton setTitleColor:[UIColor colorWithRed:248/255.0f green:144/255.0f blue:34/255.0f alpha:1] forState:UIControlStateNormal];
        
        UIButton *info = codeTimer.userInfo;
        //交互性开启
        info.enabled = YES;
        yzButton.userInteractionEnabled = YES;
        [self.timer invalidate];
    }else{
        NSString *str = [NSString stringWithFormat:@"%lu秒后重新获取",self.timeCount];
        //重新获取验证码
        [yzButton setTitle:str forState:UIControlStateNormal];
        //禁止点击‘获取验证码’
        yzButton.userInteractionEnabled = NO;
    }
}


/**
 *  当光标点击‘输入电话号码’的文本框是调出键盘
 */
-(void)touchesBegan:(NSSet *)touches withEvent:(UIEvent *)event
{
    [_phone resignFirstResponder];
    
}

-(UITextField *)creatTextFieldFrame:(CGRect )frame font:(UIFont *)font placeholder:(NSString *)placeholder
{
    UITextField *textField=[[UITextField alloc]init];
    textField.frame=frame;
    textField.font=font;
    textField.placeholder=placeholder;
    textField.borderStyle=UITextBorderStyleNone;
    textField.textColor=[UIColor grayColor];
    
    return textField;
}

-(UIImageView *)createImageViewFrame:(CGRect)frame imageName:(NSString *)imageName color:(UIColor *)color
{
    UIImageView *imageView=[[UIImageView alloc]initWithFrame:frame];
    
    if (imageName)
    {
        imageView.image=[UIImage imageNamed:imageName];
    }
    if (color)
    {
        imageView.backgroundColor=color;
    }
    
    return imageView;
}
-(UIButton *)createButtonFrame:(CGRect)frame backImageName:(NSString *)imageName title:(NSString *)title titleColor:(UIColor *)color font:(UIFont *)font target:(id)target action:(SEL)action
{
    UIButton *btn=[UIButton buttonWithType:UIButtonTypeCustom];
    btn.frame=frame;
    if (imageName)
    {
        [btn setBackgroundImage:[UIImage imageNamed:imageName] forState:UIControlStateNormal];
    }
    
    if (font)
    {
        btn.titleLabel.font=font;
    }
    
    if (title)
    {
        [btn setTitle:title forState:UIControlStateNormal];
    }
    if (color)
    {
        [btn setTitleColor:color forState:UIControlStateNormal];
    }
    if (target&&action)
    {
        [btn addTarget:target action:action forControlEvents:UIControlEventTouchUpInside];
    }
    return btn;
}


@end
